home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / oobj.h < prev    next >
C/C++ Source or Header  |  1998-09-15  |  15KB  |  466 lines

  1. /*
  2.  * @(#)oobj.h    1.80 98/07/14
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. /*
  15.  * Java object header format
  16.  */
  17.  
  18. #ifndef _OOBJ_H_
  19. #define _OOBJ_H_
  20.  
  21. #ifndef JAVA_CLASSFILE_MAGIC
  22.  
  23. #include <stddef.h>
  24.  
  25. #include "typedefs.h"
  26. #include "debug.h"
  27. #include "bool.h"
  28. #include "oobj_md.h"
  29. #include "signature.h"
  30.  
  31. #define JAVA_CLASSFILE_MAGIC              0xCafeBabe
  32.  
  33. #define JAVASRCEXT "java"
  34. #define JAVASRCEXTLEN 4
  35. #define JAVAOBJEXT "class"
  36. #define JAVAOBJEXTLEN 5
  37.  
  38. #define JAVA_VERSION     45
  39. #define JAVA_MINOR_VERSION 3
  40. #define ARRAYHEADER     long alloclen
  41.  
  42. #define HandleTo(T) typedef struct H##T { Class##T *obj; struct methodtable *methods;} H##T
  43.  
  44.  
  45. typedef long OBJECT;
  46. typedef OBJECT Classjava_lang_Object;
  47. typedef OBJECT ClassObject;
  48. HandleTo(java_lang_Object);
  49. typedef Hjava_lang_Object JHandle;
  50. typedef Hjava_lang_Object HObject;
  51.  
  52. typedef unsigned short unicode;
  53.  
  54. extern unicode    *str2unicode(char *, unicode *, long);
  55. extern char    *int642CString(int64_t number, char *buf, int buflen);
  56.  
  57. #define ALIGN(n) (((n)+3)&~3)
  58. #define UCALIGN(n) ((unsigned char *)ALIGN((int)(n)))
  59.  
  60. struct Hjava_lang_Class;    /* forward reference for some compilers */
  61. struct Classjava_lang_Class;    /* forward reference for some compilers */
  62.  
  63. typedef struct Classjava_lang_Class Classjava_lang_Class;
  64.  
  65. HandleTo(java_lang_Class);
  66. typedef struct Hjava_lang_Class ClassClass;
  67.  
  68.  
  69. struct fieldblock {
  70.     ClassClass *clazz;
  71.     char *signature;
  72.     char *name;
  73.     unsigned long ID;
  74.     unsigned short access;
  75.     union {
  76.     unsigned long offset;    /* info of data */    
  77.     OBJECT static_value;
  78.     void *static_address;
  79.     } u;
  80. };
  81.  
  82. #define fieldname(fb)    ((fb)->name)
  83. #define fieldsig(fb)     ((fb)->signature)
  84. #define fieldIsArray(fb) (fieldsig(fb)[0] == SIGNATURE_ARRAY)
  85. #define fieldIsClass(fb) (fieldsig(fb)[0] == SIGNATURE_CLASS)
  86. #define    fieldclass(fb)   ((fb)->clazz)
  87.  
  88. struct execenv;
  89.  
  90. struct methodblock {
  91.     struct fieldblock fb;
  92.     unsigned char       *code;    /* the code */
  93.     struct CatchFrame   *exception_table;
  94.     struct lineno       *line_number_table;
  95.     struct localvar     *localvar_table;
  96.  
  97.     unsigned long        code_length;
  98.     unsigned long        exception_table_length;
  99.     unsigned long        line_number_table_length;
  100.     unsigned long        localvar_table_length;
  101.  
  102.     bool_t  (*invoker)
  103.       (JHandle *o, struct methodblock *mb, int args_size, struct execenv *ee);
  104.     unsigned short       args_size;    /* total size of all arguments */
  105.     unsigned short       maxstack;    /* maximum stack usage */
  106.     unsigned short       nlocals;    /* maximum number of locals */
  107.     /* 2 spare bytes here */
  108.     void                *CompiledCode; /* it's type is machine dependent */
  109.     void                *CompiledCodeInfo; /* it's type is machine dependent */
  110.     long                 CompiledCodeFlags; /* machine dependent bits */
  111.     unsigned long        inlining;      /* possible inlining of code */
  112.     unsigned short       nexceptions;   /* number of checked exceptions */
  113.     unsigned short      *exceptions;    /* constant pool indices */
  114. #ifdef JCOV 
  115.     struct covtable      *coverage_table;
  116.     unsigned long        coverage_table_length;
  117. #endif
  118. };
  119.  
  120. struct HIOstream;
  121.  
  122. struct methodtable {
  123.     ClassClass *classdescriptor;
  124.     struct methodblock *methods[1];
  125. };
  126.  
  127. struct imethodtable { 
  128.     int icount;            /* number of interfaces to follow */
  129.     struct { 
  130.     ClassClass *classdescriptor;
  131.     unsigned long *offsets;    /* info of data */    
  132.     } itable[1];
  133. };
  134.  
  135. typedef struct {
  136.     char body[1];
  137. } ArrayOfByte;
  138. typedef ArrayOfByte ClassArrayOfByte;
  139. HandleTo(ArrayOfByte);
  140.  
  141. typedef struct {
  142.     unicode body[1];
  143. } ArrayOfChar;
  144. typedef ArrayOfChar ClassArrayOfChar;
  145. HandleTo(ArrayOfChar);
  146.  
  147. typedef struct {
  148.     signed short body[1];
  149. } ArrayOfShort;
  150. typedef ArrayOfShort ClassArrayOfShort;
  151. HandleTo(ArrayOfShort);
  152.  
  153. typedef struct {
  154.     long        body[1];
  155. } ArrayOfInt;
  156. typedef ArrayOfInt ClassArrayOfInt;
  157. HandleTo(ArrayOfInt);
  158.  
  159. typedef struct {
  160.     int64_t        body[1];
  161. } ArrayOfLong;
  162. typedef ArrayOfLong ClassArrayOfLong;
  163. HandleTo(ArrayOfLong);
  164.  
  165. typedef struct {
  166.     float       body[1];
  167. } ArrayOfFloat;
  168. typedef ArrayOfFloat ClassArrayOfFloat;
  169. HandleTo(ArrayOfFloat);
  170.  
  171. typedef struct {
  172.     double       body[1];
  173. } ArrayOfDouble;
  174. typedef ArrayOfDouble ClassArrayOfDouble;
  175. HandleTo(ArrayOfDouble);
  176.  
  177. typedef struct {
  178.     JHandle *(body[1]);
  179. } ArrayOfArray;
  180. typedef ArrayOfArray ClassArrayOfArray;
  181. HandleTo(ArrayOfArray);
  182.  
  183. typedef struct {
  184.     HObject *(body[1]);
  185. } ArrayOfObject;
  186. typedef ArrayOfObject ClassArrayOfObject;
  187. HandleTo(ArrayOfObject);
  188.  
  189. typedef struct Hjava_lang_String HString;
  190.  
  191. typedef struct {
  192.     HString  *(body[1]);
  193. } ArrayOfString;
  194. typedef ArrayOfString ClassArrayOfString;
  195. HandleTo(ArrayOfString);
  196.  
  197.  
  198. /* Note: any handles in this structure must also have explicit
  199.    code in the ScanClasses() routine of the garbage collector
  200.    to mark the handle. */
  201. struct Classjava_lang_Class {
  202.     /* Things following here are saved in the .class file */
  203.     unsigned short         major_version;
  204.     unsigned short         minor_version;
  205.     char                    *name;
  206.     char                    *super_name;
  207.     char                    *source_name;
  208.     ClassClass              *superclass;
  209.     ClassClass              *HandleToSelf;
  210.     struct Hjava_lang_ClassLoader *loader;
  211.     struct methodblock        *finalizer;
  212.  
  213.     union cp_item_type      *constantpool;
  214.     struct methodblock      *methods;
  215.     struct fieldblock       *fields;
  216.     short                   *implements;
  217.  
  218.     struct methodtable      *methodtable;
  219.     struct methodtable        *methodtable_mem;
  220.     struct fieldblock      **slottable;
  221.  
  222.     HString            *classname;
  223.  
  224.     union {
  225.     struct {
  226.         unsigned long    thishash;      /* unused */
  227.         unsigned long    totalhash;      /* unused */
  228.     } cbhash;
  229.     struct {
  230.         unsigned char    typecode;      /* VM typecode */
  231.         char        typesig;      /* signature constant */
  232.         unsigned char    slotsize;      /* (bytes) in slot */
  233.         unsigned char    elementsize;      /* (bytes) in array */
  234.         unsigned long    xxspare;
  235.     } cbtypeinfo;
  236.     } hashinfo;
  237.  
  238.     unsigned short           constantpool_count;  /* number of items in pool */
  239.     unsigned short           methods_count;       /* number of methods */
  240.     unsigned short           fields_count;        /* number of fields */
  241.     unsigned short           implements_count;    /* number of protocols */
  242.  
  243.     unsigned short           methodtable_size;    /* the size of method table */
  244.     unsigned short           slottbl_size;        /* size of slottable */
  245.     unsigned short           instance_size;       /* (bytes) of an instance */
  246.  
  247.     unsigned short access;           /* how this class can be accesses */
  248.     unsigned short flags;         /* see the CCF_* macros */
  249.     struct HArrayOfObject   *signers;
  250.     struct   imethodtable   *imethodtable;
  251.  
  252.     void                    *init_thread; /* EE of initializing thread */    
  253.  
  254.     ClassClass              *last_subclass_of;
  255.     void            *reserved_for_jit;
  256. #ifdef JCOV
  257.     char                    *absolute_source_name;
  258.     int64_t                timestamp;
  259. #endif
  260. };
  261.  
  262.  
  263. extern void FreeClass(ClassClass *cb);
  264. extern void MakeClassSticky(ClassClass *cb);
  265.  
  266. #define cbAccess(cb)          ((unhand(cb))->access)
  267. #define cbClassname(cb)       ((unhand(cb))->classname)
  268. #define cbConstantPool(cb)    ((unhand(cb))->constantpool)
  269. #define cbConstantPoolCount(cb) ((unhand(cb))->constantpool_count)
  270. #define    cbFields(cb)          ((unhand(cb))->fields)
  271. #define    cbFieldsCount(cb)     ((unhand(cb))->fields_count)
  272. #define cbFinalizer(cb)       ((unhand(cb))->finalizer)
  273. #define cbFlags(cb)           ((unhand(cb))->flags)
  274. #define cbHandle(cb)          (cb)
  275. #define cbImplements(cb)      ((unhand(cb))->implements)
  276. #define cbImplementsCount(cb) ((unhand(cb))->implements_count)
  277. #define cbInstanceSize(cb)    ((unhand(cb))->instance_size)
  278. #define cbIntfMethodTable(cb) ((unhand(cb))->imethodtable)
  279. #define cbLastSubclassOf(cb)  ((unhand(cb))->last_subclass_of)
  280. #define    cbLoader(cb)          ((unhand(cb))->loader)
  281. #define cbMajorVersion(cb)    ((unhand(cb))->major_version)
  282. #define    cbMethods(cb)         ((unhand(cb))->methods)
  283. #define    cbMethodsCount(cb)    ((unhand(cb))->methods_count)
  284. #define cbMethodTable(cb)     ((unhand(cb))->methodtable)
  285. #define cbMethodTableMem(cb)  ((unhand(cb))->methodtable_mem)
  286. #define cbMethodTableSize(cb) ((unhand(cb))->methodtable_size)
  287. #define cbMinorVersion(cb)    ((unhand(cb))->minor_version)
  288. #define cbName(cb)            ((unhand(cb))->name)
  289. #define cbSigners(cb)         ((unhand(cb))->signers)
  290. #define cbSlotTable(cb)       ((unhand(cb))->slottable)
  291. #define cbSlotTableSize(cb)   ((unhand(cb))->slottbl_size)
  292. #define cbSourceName(cb)      ((unhand(cb))->source_name)
  293. #define cbSuperclass(cb)      ((unhand(cb))->superclass)
  294. #define cbSuperName(cb)       ((unhand(cb))->super_name)
  295. #define cbThisHash(cb)        ((unhand(cb))->hashinfo.cbhash.thishash)
  296. #define cbTotalHash(cb)       ((unhand(cb))->hashinfo.cbhash.totalhash)
  297. #define cbInitThread(cb)      ((unhand(cb))->init_thread)
  298.  
  299. #ifdef JCOV
  300. #define cbAbsoluteSourceName(cb) ((unhand(cb))->absolute_source_name)
  301. #define cbTimestamp(cb)       ((unhand(cb))->timestamp)
  302. #endif
  303.  
  304. #define cbIsInterface(cb)     ((cbAccess(cb) & ACC_INTERFACE) != 0)
  305.  
  306. /* These are currently only valid for primitive types */
  307. #define    cbIsPrimitive(cb)      (CCIs(cb, Primitive))
  308. #define cbTypeCode(cb)           ((unhand(cb))->hashinfo.cbtypeinfo.typecode)
  309. #define cbTypeSig(cb)           ((unhand(cb))->hashinfo.cbtypeinfo.typesig)
  310. #define cbSlotSize(cb)           ((unhand(cb))->hashinfo.cbtypeinfo.slotsize)
  311. #define cbElementSize(cb)      ((unhand(cb))->hashinfo.cbtypeinfo.elementsize)
  312.  
  313. extern char *classname2string(char *str, char *dst, int size);
  314.  
  315. #define twoword_static_address(fb) ((fb)->u.static_address)
  316. #define normal_static_address(fb)  (&(fb)->u.static_value)
  317.  
  318. /* ClassClass flags */
  319. #define CCF_IsSysLock     0x01  /* any instance treated as a "system" lock */
  320. #define CCF_IsResolved      0x02    /* has <clinit> been run? */
  321. #define CCF_IsError      0x04    /* Resolution caused an error */
  322. #define CCF_IsSoftRef      0x08    /* whether this is class Ref or subclass */
  323. #define CCF_IsInitialized 0x10    /* whether this is class has been inited */
  324. #define CCF_IsLinked      0x20    /* Has symbolic entries been linked */
  325. #define CCF_IsVerified    0x40    /* has the verifier been run */
  326.  
  327. #define CCF_IsPrimitive   0x100    /* if pseudo-class for a primitive type */
  328. #define CCF_IsReferenced  0x200 /* Class is in use */
  329. #define CCF_IsSticky      0x400 /* Don't unload this class */
  330.  
  331. #define CCIs(cb,flag) (((unhand(cb))->flags & CCF_Is##flag) != 0)
  332. #define CCSet(cb,flag) ((unhand(cb))->flags |= CCF_Is##flag)
  333. #define CCClear(cb,flag) ((unhand(cb))->flags &= ~CCF_Is##flag)
  334.  
  335. /* map from pc to line number */
  336. struct lineno {
  337.     unsigned long pc, 
  338.     line_number;
  339. };
  340.  
  341. extern struct lineno *lntbl;
  342. extern long lntsize, lntused;
  343.  
  344. #ifdef JCOV
  345. struct covtable {
  346.     unsigned long pc, 
  347.       type,
  348.       where,
  349.       count;
  350. };
  351. #endif
  352.  
  353. /* Symbol table entry for local variables and parameters.
  354.    pc0/length defines the range that the variable is valid, slot
  355.    is the position in the local variable array in ExecEnv.
  356.    nameoff and sigoff are offsets into the string table for the
  357.    variable name and type signature.  A variable is defined with
  358.    DefineVariable, and at that time, the node for that name is
  359.    stored in the localvar entry.  When code generate is completed
  360.    for a particular scope, a second pass it made to replace the
  361.    src node entry with the correct length. */
  362.  
  363. struct localvar {
  364.     long pc0;            /* starting pc for this variable */
  365.     long length;        /* -1 initially, end pc - pc when we're done */
  366.     short nameoff;        /* offset into string table */
  367.     short sigoff;        /* offset into string table */
  368.     long slot;            /* local variable slot */
  369. };
  370.  
  371. /* Try/catch is implemented as follows.  On a per class basis,
  372.    there is a catch frame handler (below) for each catch frame
  373.    that appears in the source.  It contains the pc range of the
  374.    corresponding try body, a pc to jump to in the event that that
  375.    handler is chosen, and a catchType which must match the object
  376.    being thrown if that catch handler is to be taken.
  377.  
  378.    The list of catch frames are sorted by pc.  If one range is
  379.    inside another, then outer most range (the one that encompasses
  380.    the other) appears last in the list.  Therefore, it is possible
  381.    to search forward, and the first one that matches is the
  382.    innermost one.
  383.  
  384.    Methods with catch handlers will layout the code without the
  385.    catch frames.  After all the code is generated, the catch
  386.    clauses are generated and table entries are created.
  387.  
  388.    When the class is complete, the table entries are dumped along
  389.    with the rest of the class. */
  390.  
  391. struct CatchFrame {
  392.     long    start_pc, end_pc;    /* pc range of corresponding try block */
  393.     long    handler_pc;            /* pc of catch handler */
  394.     void*   compiled_CatchFrame; /* space to be used by machine code */
  395.     short   catchType;            /* type of catch parameter */
  396. };
  397.  
  398. #define MC_SUPER        (1<<5)
  399. #define MC_NARGSMASK    (MC_SUPER-1)
  400. #define MC_INT          (0<<6)
  401. #define MC_FLOAT        (1<<6)
  402. #define MC_VOID         (2<<6)
  403. #define MC_OTHER        (3<<6)
  404. #define MC_TYPEMASK     (3<<6)
  405.  
  406. enum {
  407.     CONSTANT_Utf8 = 1,
  408.     CONSTANT_Unicode,        /* unused */
  409.     CONSTANT_Integer,
  410.     CONSTANT_Float,
  411.     CONSTANT_Long,      
  412.     CONSTANT_Double,
  413.     CONSTANT_Class,
  414.     CONSTANT_String,
  415.     CONSTANT_Fieldref,
  416.     CONSTANT_Methodref,
  417.     CONSTANT_InterfaceMethodref,
  418.     CONSTANT_NameAndType
  419. };
  420.  
  421. union cp_item_type {
  422.     int i;
  423.     float f;
  424.     char *cp;
  425.     unsigned char *type;        /* for type table */
  426.     ClassClass *clazz;
  427.     struct methodblock *mb;
  428.     struct fieldblock *fb;
  429.     struct Hjava_lang_String *str;
  430.     void *p;                    /* for very rare occassions */
  431. };
  432.  
  433. typedef union cp_item_type cp_item_type;
  434.  
  435. #define CONSTANT_POOL_ENTRY_RESOLVED 0x80
  436. #define CONSTANT_POOL_ENTRY_TYPEMASK 0x7F
  437. #define CONSTANT_POOL_TYPE_TABLE_GET(cp,i) (((unsigned char *)(cp))[i])
  438. #define CONSTANT_POOL_TYPE_TABLE_PUT(cp,i,v) (CONSTANT_POOL_TYPE_TABLE_GET(cp,i) = (v))
  439. #define CONSTANT_POOL_TYPE_TABLE_SET_RESOLVED(cp,i) \
  440.     (CONSTANT_POOL_TYPE_TABLE_GET(cp,i) |= CONSTANT_POOL_ENTRY_RESOLVED)
  441. #define CONSTANT_POOL_TYPE_TABLE_IS_RESOLVED(cp,i) \
  442.     ((CONSTANT_POOL_TYPE_TABLE_GET(cp,i) & CONSTANT_POOL_ENTRY_RESOLVED) != 0)
  443. #define CONSTANT_POOL_TYPE_TABLE_GET_TYPE(cp,i) \
  444.         (CONSTANT_POOL_TYPE_TABLE_GET(cp,i) & CONSTANT_POOL_ENTRY_TYPEMASK)
  445.  
  446. #define CONSTANT_POOL_TYPE_TABLE_INDEX 0
  447. #define CONSTANT_POOL_UNUSED_INDEX 1
  448.  
  449. /* The following are used by the constant pool of "array" classes. */
  450.  
  451. #define CONSTANT_POOL_ARRAY_DEPTH_INDEX 1
  452. #define CONSTANT_POOL_ARRAY_TYPE_INDEX 2
  453. #define CONSTANT_POOL_ARRAY_CLASS_INDEX 3
  454. #define CONSTANT_POOL_ARRAY_LENGTH 4
  455.  
  456. /* 
  457.  * Package shorthand: this isn't obviously the correct place.
  458.  */
  459. #define JAVAPKG         "java/lang/"
  460. #define JAVAIOPKG       "java/io/"
  461. #define JAVANETPKG      "java/net/"
  462.  
  463. #endif
  464.  
  465. #endif /* !_OOBJ_H_ */
  466.